#处理txt，excel，yaml，json文件
#txt
#文件对象,读文件:r:反转义
import json
import os
from fileinput import filename
import openpyxl
from openpyxl import Workbook
from openpyxl.worksheet.worksheet import Worksheet
from common.doLog import DoLog

dl = DoLog()
myLogger = dl.getLogger(__name__)

myPath=r'E:\work\codings\python\2506\echop\datas'
# # enconding='utf-8' 可以打印中文，没有无法打印中文
# f=open(frilName,encoding='utf-8',mode='r+')
# print(f,type(f))
# print(f.read())
# f.write('的风格和健康,八十九年的春节')
#
# # f.close()
#
class DoFile():
    # def readPNG(self):
    #     ptoonName=r'E:\work\codings\python\2506\pyBase\480a19f7847e04ac492c58892909619c.jpg'
    #     with open(ptoonName,mode='r+b') as g:
    #         print(g,type(g))
    #         myPNG=g.read()
    #         return myPNG
    #
    # def doPNGWrite(self,fname,data):
    #     ptoonName=rf'E:\work\codings\python\2506\pyBase\{fname}'
    #     with open(ptoonName,mode='w+b')as g:
    #         g.write(data)
    #字典转换成json格式的字符串
    # def doDicTOJsonStr(self,d):
    #     s=json.dumps(d)
    #     try:
    #         print(s,type(s))
    #     except Exception as e:
    #         print(f'发生异常，信息{e}')
    #         myLogger.error(f'发生异常，信息{e}')
    #     else:
    #         print('未发生异常')
    #         myLogger.info('未发生异常')
    #     finally:
    #         print('额不知道')
    #         myLogger.info('额不知道')
    #     return s
    #
    # #json格式的字符串转换成字典
    # def doJsonTODicStr(self,jionS):
    #     d=json.dumps(jionS)
    #     try:
    #         print(d,type(d))
    #     except Exception as e:
    #         print(f'发生异常，信息{e}')
    #         myLogger.error(f'发生异常，信息{e}')
    #     else:
    #         print('未发生异常')
    #         myLogger.info('未发生异常')
    #     finally:
    #         print('额不知道')
    #         myLogger.info('额不知道')
    #     return d
    #
    # #字典中内容保存成功json格式文件
    # def doDicTOJsonStr(self,d,fname):
    #     fileName = os.path.join(myPath,fname)
    #     with open(fileName,mode='a',encoding='utf-8')as f:
    #         json.dump(d,f)
    # json格式文件提取出并转成字典

    #处理excel文件，读，写
    def doExcelRead(self,fname):
        fileName = os.path.join(myPath, fname)

        wk:Workbook=openpyxl.load_workbook(fileName)
        st:Workbook=wk["add"]
        try:
            print(st,type(st))
            ss=st.iter_rows(values_only=True)
            print(ss)
            print(range(10))
        except Exception as e:
            print(f'文件{fileName}{"ad"}发生异常，信息是:{e}')
            myLogger.error(f'发生异常，信息{e}')
        else:
            print('未发生异常')
            myLogger.info('未发生异常')
        finally:
            print('额不知道')
            myLogger.info('额不知道')
        l=[i for i in ss]
        print(f'{l}')
        return l

    #处理excel文件，读
    def doExcelReadDIV(self,fname,stname,minRow=None,maxRow=None,minCol=None,maxCol=None):
        fileName = os.path.join(myPath, fname)

        wk:Workbook=openpyxl.load_workbook(fileName)
        st:Worksheet=wk[stname]
        try:
            print(st,type(st))
            ss=st.iter_rows(min_row=minRow,
                            max_row=maxRow,
                            min_col=minCol,
                            max_col=maxCol,
                            values_only=True)
        except Exception as e:
            print(f'文件{fileName}{stname}发生异常，信息是:{e}')
            myLogger.error(f'发生异常，信息{e}')
        else:
            print('未发生异常')
            myLogger.info('未发生异常')
        finally:
            print('额不知道')
            myLogger.info('额不知道')
        l=[i for i in ss]
        print(f'{l}')
        return l

    #处理excel文件，写
    # def doExcelWrite(self,fname,stname,ss):
    #     fileName = os.path.join(myPath, fname) #新文件名
    #
    #     wk=openpyxl.Workbook()
    #     st:Worksheet=wk.create_sheet(stname,1)
    #     try:
    #         st.cell(1,1,ss) #写一个单元格
    #     except Exception as e:
    #         print(f'文件{fileName}{stname}发生异常，信息是:{e}')
    #         myLogger.error(f'发生异常，信息{e}')
    #     else:
    #         print('未发生异常')
    #         myLogger.info('未发生异常')
    #     finally:
    #         print('额不知道')
    #         myLogger.info('额不知道')
    #     wk.save(fileName) #保存

if __name__ == '__main__':
    df=DoFile()
    # reader = df.doExcelReadDIV(__name__)
    # d={'name':'zhdn','age':20}
    # s=df.doJsonTODicStr(d)
    # ss={'name':'zhdn','age':20,'sex':'aa'}
    # ss = df.doJsonTODicStr(ss)
    # df.doDicTOJsonStr(d, "test.json")
    # df.doExcelRead("jia.xlsx")
    df.doExcelReadDIV('ecshop登录用例.xlsx', 'sheet1', 2, 4, 1, 5)
    # df.doExcelReadDIV('jiaa.xlsx', 'cheng', 2, 24, 2, 5)
    # df.doExcelReadDIV('jiaa.xlsx','jian',2,23,2,5)
    # df.doExcelReadDIV('jiaa.xlsx','add',2,23,2,5)
    # reader = df.doExcelReadDIV('jiaa.xlsx','add',2)
    # print(reader)
    # df.doExcelWrite('jia.xlsx','add','你好')
    # print(df.readPNG())
    # sss=df.readPNG()
    # ptoonName = 'pic3.png'
    # df.doPNGWrite(ptoonName,sss)

